<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="IP地址" prop="commentsIp">
        <el-input
          v-model="queryParams.commentsIp"
          placeholder="请输入IP地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IP归属地" prop="commentsIpadderss">
        <el-input
          v-model="queryParams.commentsIpadderss"
          placeholder="请输入IP归属地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="浏览器" prop="commentsBrowser">
        <el-input
          v-model="queryParams.commentsBrowser"
          placeholder="请输入浏览器"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="系统" prop="commentsSystem">
        <el-input
          v-model="queryParams.commentsSystem"
          placeholder="请输入系统"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="commentsStatus">
        <el-select v-model="queryParams.commentsStatus" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.nav_is_show"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="被回复者" prop="bUsername">
        <el-input
          v-model="queryParams.bUsername"
          placeholder="请输入被回复者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['navigation:comments:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['navigation:comments:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['navigation:comments:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commentsList" @selection-change="handleSelectionChange" :default-sort="defaultSort" @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" sortable="custom" :sort-orders="['descending', 'ascending']" />
      <el-table-column label="回复ID" align="center" prop="cid">
        <template slot-scope="scope">
          {{ (scope.row.cid == null) ? "顶级评论":scope.row.cid }}
        </template>
      </el-table-column>
      <el-table-column label="被回复者" align="center" prop="bUsername">
        <template slot-scope="scope">
          {{ (scope.row.bUsername == null || scope.row.bUsername == '') ? "无":scope.row.bUsername }}
        </template>
      </el-table-column>
      <el-table-column label="链接id" align="center" prop="webId" />
      <el-table-column label="内容" align="center" prop="commentsText">
        <template slot-scope="scope">
          <el-popover placement="top" width="300" trigger="hover">
            <div>{{ scope.row.commentsText }}</div>
            <el-button slot="reference" type="text">查看详情</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="commentsNickName" />
      <el-table-column label="头像" align="center" prop="commentsAvatar" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.commentsAvatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="邮箱" align="center" prop="commentsEmail" />
      <el-table-column label="IP地址" align="center" prop="commentsIp" />
      <el-table-column label="IP归属地" align="center" prop="commentsIpadderss" />
      <el-table-column label="浏览器" align="center" prop="commentsBrowser" />
      <el-table-column label="系统" align="center" prop="commentsSystem" />
      <el-table-column label="状态" align="center" prop="commentsStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.nav_is_show" :value="scope.row.commentsStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['navigation:comments:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['navigation:comments:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评论列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="状态" prop="commentsStatus">
          <el-select v-model="form.commentsStatus" placeholder="请选择状态" clearable>
            <el-option
              v-for="dict in dict.type.nav_is_show"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="内容" prop="commentsText">
          <el-input v-model="form.commentsText" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listComments, getComments, delComments, addComments, updateComments } from "@/api/navigation/comments";

export default {
  name: "Comments",
  dicts: ['nav_is_show'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评论列表表格数据
      commentsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 默认排序
      defaultSort: {prop: 'createTime', order: 'descending'},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        commentsText: null,
        commentsIp: null,
        commentsIpadderss: null,
        commentsBrowser: null,
        commentsSystem: null,
        commentsStatus: null,
        createBy: null,
        bUsername: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        webId: [
          { required: true, message: "链接id不能为空", trigger: "blur" }
        ],
        commentsIp: [
          { required: true, message: "IP地址不能为空", trigger: "blur" }
        ],
        commentsIpadderss: [
          { required: true, message: "IP归属地不能为空", trigger: "blur" }
        ],
        commentsBrowser: [
          { required: true, message: "浏览器不能为空", trigger: "blur" }
        ],
        commentsSystem: [
          { required: true, message: "系统不能为空", trigger: "blur" }
        ],
        commentsStatus: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询评论列表列表 */
    getList() {
      this.loading = true;
      listComments(this.queryParams).then(response => {
        this.commentsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pId: null,
        webId: null,
        commentsText: null,
        commentsIp: null,
        commentsIpadderss: null,
        commentsBrowser: null,
        commentsSystem: null,
        commentsStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        bUsername: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getComments(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改评论列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateComments(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addComments(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除评论列表编号为"' + ids + '"的数据项？').then(function() {
        return delComments(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('navigation/comments/export', {
        ...this.queryParams
      }, `comments_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
